<?php
namespace app\zjs\controller;
use app\admin\model\Customer;
use app\common\model\Order_log;
//use app\common\traits\Wechat;
use think\Db;
use think\Facade\Session;
use app\common\model\Order;
use app\admin\model\Orders;
class Tj extends Zjs {
//    use Wechat;
    protected function _initialize() {
        parent::_initialize();
        if(!session('user_id')){
            $this->redirect(url('publics/bangding'));
        }

//        session('user_id',1);
    }
    public function index() {
        return $this->fetch('index/recommend');
    }
    //报名接收方法 接收到后推送到系统 
    public function react() {
        if($this->request->isAjax()){
            $data= input('post.');
            $user = Customer::get(['id'=>$data['user_id'],'status'=>1]);//推荐人
            if(empty($user)){
                $this->error('没有绑定或禁用的用户暂时不能推荐顾客！');
            }
            //中间表查重
            $info = Db::name('orders')->where('tel',$data['tel'])->field('tel,user_id')->find();
            if(!empty($info)){
                $info['user_id'] == session('user_id')?$this->error('您已经介绍过该顾客了，如有疑问，请联系客服！'):$this->error('该顾客已经有人介绍了，如有疑问，请联系客服！');
                exit;
            }
            $orders = new Orders();
            $orders->user_id = $user->id;
            $orders->name    = $data['name'];
            $orders->tel     = $data['tel'];
            $orders->bz      = $data['bz'];
            $orders->state   = 1;
            $orders->ywstate = 4;
            $result = $orders->save();//新建中间表
            if(false === $result){
                $this->error( $orders->getError());exit;// 验证失败 输出错误信息
            }
            $orders_id = $orders->id;
            $chachong = $this->getPhone($data['tel']);
            $sendhuashu = new \app\common\model\SendHuaShua();
            if($chachong['status'] == 1){//没有重复
                $order = new Order();
                $order->daoru    = 3;
                $order->name     = $data['name'];
                $order->maphone  = $data['tel'];
                $order->ywstate  = 4;
                $order->status   = 1;
                $order->qdbz     = $data['bz'];
                $order->qudao    = $user->attr;
                $order->tjr_id   = $user->id;
                $order->bumen    = $user->bumen;
                $order->jdtime   = time();
                $order->qd_depar = 6;
                $order->fenlei   = 83;
                $order->validate(true)->save();//新建订单表

                $order_id = $order->getLastInsID();
                $order_log = new Order_log();
                $order_log->log(['id'=>$order_id,'maphone'=>$data['tel'],'operation'=>1],2);//订单表日志
                Db::name('orders')->update(['id'=>$orders_id,'order_id'=>$order_id,'status'=>1]);//修改中间表
                $sendhuashu->sendMsg(['id'=>$orders_id,'hslx'=>2]);//给转介绍人发微信
                $wxdata = ['id'=>$order_id,'first'=>'转介绍新单,自动审核成功！'];
                $res = $orders->pushWxToYyjl($wxdata);//转介绍新建单，给邀约经理发微信
                $info = "<span style='color:green;'>系统</span>审核订单成功！订单进入待分配流程！";
                dellZjsLog(['order_id'=>$orders_id,'info'=>$info,'status'=>4,'operation'=>1,'czry'=>session('user_id')],$res);//中间表日志
                $this->success('推荐成功，感谢您对玛瑞莎坚定的支持！');exit;
            }else{
//                    Db::name('orders')->update(['id'=>$orders_id,'status'=>2]);//修改中间表
                $sendhuashu->sendMsg(['id'=>$orders_id,'hslx'=>3]);//给转介绍人发微信
                $res = $orders->pushWxToYyjlSh($orders_id);//转介绍新建单，给邀约经理发微信
                $info ="<span style='color:green;'>系统</span>审核中！等待审核！";
                dellZjsLog(['order_id'=>$orders_id,'info'=>$info,'status'=>4,'operation'=>1,'czry'=>session('user_id')],$res);//中间表日志
                $this->success('电话重复,订单审核中，感谢您对玛瑞莎坚定的支持！');exit;
            }
        }else{
            $this->error('提交的方式不对！');
        }
    }

    /**
     * 远程post请求
     * @param $url
     * @param $post_data
     * @return mixed
     */
    public static function getcurl($url, $post_data){

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $output = curl_exec($ch);
        $httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
        curl_close($ch);
        return $output;
    }

    //获取PHONE
    public function getPhone($phone)
    {
//        $phone  = $this->request->param('phone');
        trim($phone);
        if(!empty($phone)){
            $user =  Db::table('jtds_order')->where(['status'=>1,'maphone|otherphone'=>$phone])->find();
        }
        if(!empty($user)){
            return ['status'=>0,'msg'=>'电话号码重复，请联系客服！','icon'=>2];
        }else{
            return ['status'=>1,'msg'=>'恭喜您电话号码不重复','icon'=>1];
        }
    }
}
